package com.ldgame.ai;

import java.util.ArrayList;

import org.lwjgl.util.vector.Vector2f;
import org.newdawn.slick.Color;

import com.ldgame.entities.Bullet;
import com.ldgame.entities.City;
import com.ldgame.entities.Entity;
import com.ldgame.entities.Player;
import com.ldgame.entities.Team;
import com.ldgame.sound.Sound;
import com.ldgame.sound.SoundSystem;
import com.ldgame.utils.Timer;

public class StandardAI implements AI {
	private Entity entity;
	private Entity [] entities;
	private Player player;
	private City [] cities;
	private ArrayList<Bullet> bullets;
	private int targetCity;
	private int targetBuilding;
	
	private float moveTimer = (float)Math.random();
	private float shootTimer = (float)Math.random();
	
	public StandardAI(Entity entity, Entity [] entities, City [] cities, ArrayList<Bullet> bullets, Player player) {
		this.entity = entity;
		this.entities = entities;
		this.cities = cities;
		this.player = player;
		this.bullets = bullets;
		targetCity = (int)(Math.random() * cities.length);
		targetBuilding = (int)(Math.random() * cities[targetCity].getBuildings().length);
	}

	@Override
	public void sync() {
		moveTimer += Timer.getLoopTime();
		shootTimer += Timer.getLoopTime();
	}
	
	@Override
	public void processMovement() {
		if (entity.getDeathTimer() >= 30.0f) {
			targetCity = (int)(Math.random() * cities.length);
			targetBuilding = (int)(Math.random() * cities[targetCity].getBuildings().length);
		}
		
		if (entity.isEnabled()) {
			if (entity.isSelected()) {
				if (Vector2f.sub(entity.getPosition(), player.getPosition(), null).length() > 128.0f) {
					entity.setTheta((float)(Math.PI + Math.atan2(entity.getPosition().y - player.getPosition().y,
													  			 entity.getPosition().x - player.getPosition().x)));
				} else {
					if (moveTimer > 2.0f) {
						entity.setTheta(2.0f * (float)(Math.random() * Math.PI));
						moveTimer = 0.0f;
					}
				}
			} else {
				if (Vector2f.sub(entity.getPosition(), cities[targetCity].getBuildings()[targetBuilding].getPosition(), null).length() > 256.0f) {
					entity.setTheta((float)(Math.PI + Math.atan2(entity.getPosition().y - cities[targetCity].getBuildings()[targetBuilding].getPosition().y,
																 entity.getPosition().x - cities[targetCity].getBuildings()[targetBuilding].getPosition().x)));
				} else {
					if (moveTimer > 2.0f) {
						entity.setTheta(2.0f * (float)(Math.random() * Math.PI));
						moveTimer = 0.0f;
					}
				}
			}
		}
	}

	@Override
	public void shoot() {
		if (shootTimer > 1.0f) {
			if (Vector2f.sub(entity.getPosition(), player.getPosition(), null).length() < 600.0f &&
				entity.getTeam() != player.getTeam() &&
				Math.random() < 0.5) {
				float theta = (float)Math.atan2(entity.getPosition().y - player.getPosition().y,
												entity.getPosition().x - player.getPosition().x);

				createBullet((float)Math.PI + theta + 0.1f * ((float)Math.random() - 0.5f));
				shootTimer = 0.0f;
				return;
			}
			
			for (Entity entity2 : entities) {
				if (entity2.isEnabled() &&
					entity.getTeam() != entity2.getTeam() &&
					Vector2f.sub(entity.getPosition(), entity2.getPosition(), null).length() < 600.0f) {
					float theta = (float)Math.atan2(entity.getPosition().y - entity2.getPosition().y,
													entity.getPosition().x - entity2.getPosition().x);
					
					createBullet((float)Math.PI + theta + 0.1f * ((float)Math.random() - 0.5f));
					break;
				}
			}
			shootTimer = 0.0f;
		}
	}
	
	private void createBullet(float aimTheta) {
		if (Vector2f.sub(entity.getPosition(), player.getPosition(), null).length() < 500.0f) {
			SoundSystem.playSound(Sound.LASER);
		}
		
		Bullet bullet = new Bullet(entity.getTeam());
		bullet.setSpeed(300.0f);
		bullet.getSprite().setTexCoord(new Vector2f(8.0f, 32.0f));
//		bullet.getSprite().setColor((source.getTeam() == Team.TINY_HUMAN) ? new Color(0.0f, 0.2f, 0.6f, 1.0f) : new Color(1.0f, 0.0f, 0.0f, 1.0f));
		bullet.getSprite().setColor(new Color(1.0f, 1.0f, 1.0f, 1.0f));
		bullet.getSprite().setSpriteState((entity.getTeam() == Team.TINY_HUMAN) ? 0 : 1);
		bullet.setPosition(new Vector2f(entity.getPosition().x + 16.0f * (float)Math.cos(aimTheta),
										entity.getPosition().y + 16.0f * (float)Math.sin(aimTheta)));
		bullet.setTheta(aimTheta);
		bullet.getSprite().setCamPosition(player.getPosition());
		bullet.setMoving(true);
		bullets.add(bullet);
		
//		Bullet bullet = new Bullet(entity.getTeam());
//		bullet.setSpeed(300.0f);
//		//bullet.getSprite().setColor((entity.getTeam() == Team.TINY_HUMAN) ? new Color(0.0f, 0.0f, 1.0f, 1.0f) : new Color(1.0f, 0.0f, 0.0f, 1.0f));
//		bullet.getSprite().setColor(new Color(1.0f, 1.0f, 1.0f, 1.0f));
//		bullet.getSprite().setSpriteState((entity.getTeam() == Team.TINY_HUMAN) ? 0 : 1);
//		bullet.setPosition(new Vector2f(entity.getPosition().x + 8.0f * (float)Math.cos(aimTheta),
//										entity.getPosition().y + 8.0f * (float)Math.sin(aimTheta)));
//		bullet.setTheta(aimTheta);
//		bullet.getSprite().setCamPosition(player.getPosition());
//		bullet.setMoving(true);
//		bullets.add(bullet);
	}
}
